package com.swz.dcrm.ui.mine;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import com.bigkoo.pickerview.TimePickerView;
import com.swz.dcrm.model.Announcement;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.AnnouncementViewModel;
import com.swz.dcrm.util.DateUtils;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Date;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class AnnounceFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_right)
    TextView tvRight;
    private TimePickerView pickerView;
    private int current;

    @Inject
    AnnouncementViewModel announcementViewModel;

    public static AnnounceFragment newInstance() {

        return new AnnounceFragment();
        // Required empty public constructor
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_add_announcement;
    }
    @Override
    public void onLoadRetry() {

    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.announce_title));
        tvRight.setText(getString(R.string.announce_send));
        tvStartTime.setText(DateUtils.dateFormat(new Date(),"yyyy-MM-dd"));
        tvEndTime.setText(DateUtils.dateFormat(new Date(),"yyyy-MM-dd"));
        pickerView=getTimePickerView((date, v) -> {
            if(current==0){
                tvStartTime.setText(DateUtils.dateFormat(date,"yyyy-MM-dd"));
            }else {
                tvEndTime.setText(DateUtils.dateFormat(date,"yyyy-MM-dd"));
            }
        }).build();

        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick({R.id.ll_start,R.id.ll_end})
    public void pick(View view){

        switch (view.getId()){
            case R.id.ll_start:
                current=0;
                break;
            case R.id.ll_end:
                current=1;
                break;
        }
        pickerView.show();
    }
    @OnClick(R.id.tv_right)
    public void save(){
        String content=etContent.getText().toString().trim();
        if(TextUtils.isEmpty(content)){
            showToast(getString(R.string.add_customer_hint)+getString(R.string.announce_content));
            return;
        }
        if(TextUtils.isEmpty(tvStartTime.getText().toString())){
            showToast(getString(R.string.add_customer_hint_select)+getString(R.string.announce_start_time));
            return;
        }
        if(TextUtils.isEmpty(tvEndTime.getText().toString())){
            showToast(getString(R.string.add_customer_hint_select)+getString(R.string.announce_end_time));
            return;
        }
        Date startDate=DateUtils.dateParse(tvStartTime.getText().toString(),"yyyy-MM-dd");
        Date endDate=DateUtils.dateParse(tvEndTime.getText().toString(),"yyyy-MM-dd");
        if(startDate.after(endDate)){
            showToast("开始时间不能大于结束时间");
            return;
        }
        String start=tvStartTime.getText().toString()+" 00:00:00";
        String end=tvEndTime.getText().toString()+" 23:59:59";

        announcementViewModel.add(content,start,end)
                .observe(this, announcementBaseResponse -> {
                    if(announcementBaseResponse.isSuccess()){

                        backClick();
                    }else {
                        showToast(announcementBaseResponse.getMsg());
                    }
                });
    }
}
